<template>
  <section>
    <el-row>
      <el-col class="header" :span="21">
        <div class="title">
          <span>订单管理</span>
        </div>
        <div class="addGroup"></div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="21">
        <div class="search">
          <SearchBox
            @inputChange="inputChange"
            @searchClick="searchClick"
            placeholder="查询订单"
          />
        </div>
        <el-table :data="tableData" :border="true">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="user.username" label="用户名" width="80">
          </el-table-column>
          <el-table-column prop="order_no" label="订单编号" width="130">
          </el-table-column>
          <el-table-column prop="shop.title" label="商品"> </el-table-column>
          <el-table-column prop="shop.price" label="价格"> </el-table-column>
          <el-table-column prop="quantity" label="订单数量"> </el-table-column>
          <el-table-column label="状态" width="80">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.order_state === 1"
                >进行中</el-tag
              >
              <el-tag type="info" v-else-if="scope.row.order_state === 2"
                >已收货</el-tag
              >
              <el-tag type="danger" v-else>未付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createdAt" label="下单时间" width="160">
          </el-table-column>
          <el-table-column prop="userInfo.name" label="订单姓名">
          </el-table-column>
          <el-table-column prop="userInfo.address" label="订单地址">
          </el-table-column>
          <el-table-column prop="userInfo.phone" label="订单电话">
          </el-table-column>
          <el-table-column prop="logistics" label="物流单号"> </el-table-column>
          <el-table-column label="添加单号" width="90">
            <template v-slot="scope">
              <el-button
                v-if="scope.row.order_state == 1"
                size="mini"
                type="success"
                icon="el-icon-chat-line-square"
                @click="showProgressDialog(scope.row.id)"
                >物流</el-button
              >
              <el-button
                v-if="scope.row.order_state == 2"
                size="mini"
                type="info"
                icon="el-icon-chat-line-square"
                @click="info"
                >物流</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!--添加单号的对话框-->
    <el-dialog
      title="添加物流单号"
      width="50%"
      :visible.sync="addCateDialogVisible"
      :close-on-click-modal="false"
    >
      <!--内容主体-->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        label-width="80px"
      >
        <el-form-item label="单号：" prop="logistics">
          <el-input v-model.number="addCateForm.logistics"></el-input>
        </el-form-item>
      </el-form>
      <!--底部-->
      <span slot="footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </section>
</template>
<script>
// 引入请求模块
import { $http } from "../../request/service";
import SearchBox from "../toolAssembly/SearchBox";
export default {
  name: "EvaUser",
  components: { SearchBox },
  data() {
    return {
      tableData: [],
      //监听物流单号
      addCateForm: {
        logistics: "",
      },
      id: "",
      addCateDialogVisible: false,
      addCateFormRules: {
        logistics: [
          { required: true, message: "请输入订单号码", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取商品分类列表
    getCateList() {
      $http.get(`/queryAllOrder`).then((res) => {
        if (res.code !== 200) return this.$message.error(res.data.msg);
        res.data.filter((item) => {
          item.createdAt = item.createdAt
            .replace(/T/g, "~")
            .replace(/.000Z/g, "");
        });
        // console.log(res.data);
        this.tableData = res.data;
      });
    },
    //打开添加单号Diglog
    showProgressDialog(id) {
      this.addCateDialogVisible = true;
      this.id = id;
    },
    //更新单号
    addCate() {
      if (this.addCateForm.logistics) {
        $http.put(`/queryAllOrder/${this.id}`, this.addCateForm).then((res) => {
          if (res.data.msg !== "添加成功")
            return this.$message.error(res.data.msg);
          this.addCateForm.logistics = "";
          this.addCateDialogVisible = false;
          this.$message({
            message: "添加单号成功",
            type: "success",
          });
          this.getCateList();
        });
      } else {
        this.$message({
          message: "请先输入！",
          type: "warning",
        });
      }
    },
    // 搜索订单功能
    searchClick(order_no) {
      $http
        .get(`/searchOrder?order_no=${order_no}`, { order_no })
        .then((res) => {
          res.data.filter((item) => {
            item.createdAt = item.createdAt
              .replace(/T/g, "~")
              .replace(/.000Z/g, "");
          });
          this.tableData = res.data;
        });
    },
    inputChange(blo) {
      if (blo) return this.getCateList();
    },
    info() {
      this.$message("买家已收货，无法修改物流单号");
    },
  },
};
</script>
<style scoped>
.search {
  width: 300px;
  margin-bottom: 10px;
}
.header {
  display: flex;
  align-content: center;
  justify-content: space-between;
}
.el-row {
  padding-top: 50px;
  display: flex;
  align-items: center;
}
.title {
  display: flex;
  align-items: center;
  text-align: start;
  font-size: 20px;
  font-weight: 700;
}
.searchWai {
  padding-left: 260px;
}
</style>
